package cameratricks;

import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import processing.core.PApplet;
import processing.core.PFont;
import processing.core.PImage;
import processing.core.PVector;
import processing.video.Capture;
import hypermedia.video.OpenCV;

public class DialogBubbles implements Renderer {
	Capture capture;
	PApplet p;

	PFont font;
	List<Bubble> bubbles;
	
	Date last_shuffle_time;
	
	public DialogBubbles(TrickCarousel p) {
		this.capture = p.getCapture();
		this.p = p;
		
		last_shuffle_time = new Date();
		
		String[] list = PFont.list();
		for (String s : list)
			System.out.println(s);
		
		font = p.createFont("ComicSansMS", 15);
		
		bubbles = new ArrayList<Bubble>();
		bubbles.add(new Bubble("FML.", 100, 40));
		bubbles.add(new Bubble("This will only hurt for a second..", 260, 40));
		bubbles.add(new Bubble("somebody farted..", 180, 40));
		bubbles.add(new Bubble("BOB SAGET", 150, 40));
		bubbles.add(new Bubble("grumble mumble...", 180, 40));
		bubbles.add(new Bubble("he's kinda cute...", 200, 40));
		bubbles.add(new Bubble("i REALLY need to take a piss.", 250, 40));
		bubbles.add(new Bubble("crazy bitch.", 160, 40));
		bubbles.add(new Bubble("blah blah blah...", 170, 40));
		bubbles.add(new Bubble("my name is Inigo Montoya...", 270, 40));
		bubbles.add(new Bubble("Get in mah belleh!!", 200, 40));
		bubbles.add(new Bubble("Hello World", 150, 40));
		bubbles.add(new Bubble("I am your father.", 200, 40));
		bubbles.add(new Bubble("my body, my choice.", 220, 40));
		bubbles.add(new Bubble("Houston, we have a problem", 250, 40));
		bubbles.add(new Bubble("Stay on target... STAY ON TARGET", 280, 40));
		bubbles.add(new Bubble("don't cross the streams!", 220, 40));


	}

	
	public void draw(OpenCV opencv) {
		
		Date now = new Date();
		if (now.getTime() - last_shuffle_time.getTime() > 20 * 1000) {
			shuffleBubbles();
			last_shuffle_time = now;
		}

		
		Rectangle[] faces = opencv.detect(2F, 1, OpenCV.HAAR_DO_ROUGH_SEARCH, 80,80);
		
		PImage image = opencv.image();
		image.filter(p.POSTERIZE, 5);
		p.image(image, 0, 0);
		
		List sortface =	Arrays.asList(faces);
		Collections.sort(sortface, new Comparator() {
			public int compare(Object o1, Object o2) {
				Rectangle a = (Rectangle) o1;
				Rectangle b = (Rectangle) o2;
				return a.x < b.x ? -1 : 1;
			}
			
		});
		
		faces = (Rectangle[]) sortface.toArray();
		
	    for( int i=0; i<faces.length; i++ ) {
	    	
	    	// show a dialog by the face
	    	PVector dialogCoord = new PVector(faces[i].x + faces[i].width, faces[i].y - 10);
	    	p.fill(255);
	    	p.stroke(50);
	    	p.smooth();
	    	
	    	p.ellipseMode(p.CORNER);
	    	
	    	Bubble bub = bubbles.get(i % bubbles.size());
	    	drawBubble(bub, dialogCoord);
	    }
	}
	
	private void shuffleBubbles() {
		// random swaps
		for (int i = 0; i < bubbles.size() - 1; i++) {
			Bubble a = bubbles.get(i);
			int j = (int) p.random(0, bubbles.size());
			Bubble b = bubbles.get(j );
			bubbles.set(i, b);
			bubbles.set(j, a);
		}
	}


	private void drawBubble(Bubble b, PVector dialogCoord) {
		int dialogWidth = b.w;
    	float dialogHt = b.h;
		p.ellipse(dialogCoord.x, dialogCoord.y, dialogWidth, dialogHt );
		p.ellipse(dialogCoord.x - 20, dialogCoord.y + 30, 20, 20);
		p.ellipse(dialogCoord.x - 30, dialogCoord.y + 45, 10, 10);
		
    	p.fill(0);
    	p.textFont(font);
    	p.text(b.text, dialogCoord.x + 20, dialogCoord.y + 10, dialogWidth, dialogHt);
    	
	}
	

	private class Bubble {
		private String text;
		private int w;
		private int h;

		public Bubble(String text, int w, int h) {
			this.text = text;
			this.w = w;
			this.h = h;
		}
	}
	
}
